package com.linkage.linkedlife.utils

import android.text.TextUtils
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.*

/*
* Copyright (c) 2015. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
* Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
* Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
* Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
* Vestibulum commodo. Ut rhoncus gravida arcu.
*/
/**
 * <br></br> author: chenshufei
 * <br></br> date: 15/9/15
 * <br></br> email: chenshufei2@sina.com
 */
object DateTimeUtil {
    private const val DEFALUT_DATETIMEFORMAT = "yyyy-MM-dd HH:mm:ss"
    private const val DEFALUT_DATEFORMAT = "yyyy-MM-dd"

    /**
     * 以指定的dateTimeFormat 格式化date日期时间
     * @param date
     * @param dateTimeFormat
     * @return
     */
    fun getDateTimeString(date: Date?, dateTimeFormat: String?): String {
        val sdf = SimpleDateFormat(dateTimeFormat)
        return sdf.format(date)
    }

    /**
     * 以yyyy-MM-dd hh:mm:ss 格式化date日期时间
     * @param date
     * @return
     */
    fun getDateTimeString(date: Date?): String {
        return getDateTimeString(date, DEFALUT_DATETIMEFORMAT)
    }
    /**
     * 以指定的dateTimeFormat 解析dateTimeStr日期时间字符串
     * @param dateTimeStr
     * @param dateTimeFormat
     * @return dateTimeFormat格式不规范，将返回null
     */
    /**
     * 以yyyy-MM-dd hh:mm:ss 解析获取date日期时间
     * @param dateTimeStr
     * @return
     */
    @JvmOverloads
    fun parseDateTime(
        dateTimeStr: String,
        dateTimeFormat: String? = DEFALUT_DATETIMEFORMAT
    ): Date? {
        var sdf: SimpleDateFormat? = null
        sdf = if (dateTimeStr.toUpperCase().contains("GMT")) {
            SimpleDateFormat(dateTimeFormat, Locale.US)
        } else {
            SimpleDateFormat(dateTimeFormat)
        }
        return try {
            sdf.parse(dateTimeStr)
        } catch (e: ParseException) {
            null
        }
    }

    fun stringToDateTime(strDate: String): Date? {
        return parseDateTime(strDate)
    }

    fun dateTimeToString(dateTime: Any?): String? {
        return if (dateTime != null) {
            SimpleDateFormat(DEFALUT_DATETIMEFORMAT).format(dateTime)
        } else null
    }

    fun dateTimeToYMD(dateTime: Any?): String? {
        var sourceString: String? = null
        if (dateTime != null) {
            sourceString = SimpleDateFormat(DEFALUT_DATETIMEFORMAT).format(dateTime)
            sourceString = sourceString.substring(0, 10)
            sourceString = sourceString.replace("-", "")
            return sourceString
        }
        return null
    }

    /**
     * 返回yyyy-MM-dd 格式日期
     * @param strDate
     * @return
     */
    fun getDatetimeStr(strDate: String): String {
        if (!TextUtils.isEmpty(strDate)) {
            val sdf = SimpleDateFormat(DEFALUT_DATEFORMAT)
            return sdf.format(stringToDateTime(strDate))
        }
        return ""
    }

    /**
     * 返回yyyy-MM-dd 格式日期
     * @param date java.util.data
     * @return
     */
    fun getDefaultDate(date: Date?): String {
        if (null != date) {
            val sdf = SimpleDateFormat(DEFALUT_DATEFORMAT)
            return sdf.format(date)
        }
        return ""
    }

    /**
     * yyyy-MM-dd 转 yyyy-MM-dd HH:mm:ss 格式
     * @param strDate
     * @return
     */
    fun formatYYYYMMDD2YYYYMMDDHHMMSS(strDate: String?): String {
        if (!TextUtils.isEmpty(strDate)) {
            var sdf = SimpleDateFormat(DEFALUT_DATEFORMAT)
            var parseDate: Date? = null
            try {
                parseDate = sdf.parse(strDate)
            } catch (e: ParseException) {
                e.printStackTrace()
            }
            sdf = SimpleDateFormat(DEFALUT_DATETIMEFORMAT)
            return sdf.format(parseDate)
        }
        return ""
    }

    /***
     * 将yyyy-MM-dd日期 转成格式MMdd日期
     * @param strDate
     * @return
     */
    fun getDateNoYear(strDate: String?): String? {
        if (!TextUtils.isEmpty(strDate)) {
            val sdf = SimpleDateFormat("MMdd")
            try {
                return sdf.format(SimpleDateFormat("yyyy-MM-dd").parse(strDate))
            } catch (e: ParseException) {
            }
        }
        return strDate
    }
    // 标准时间格式
    /**
     * 返回yyyy年MM月dd日格式日期
     * @param strDate
     * @return
     */
    fun getChinaDatetimeStr(strDate: String): String {
        if (!TextUtils.isEmpty(strDate)) {
            val sdf = SimpleDateFormat("yyyy年MM月dd日")
            return sdf.format(stringToDateTime(strDate))
        }
        return ""
    }

    /****
     *
     * @param date yyyy-MM-dd
     * @return MM-dd
     */
    fun getMonthDayFormat(date: String): String {
        var date = date
        if (TextUtils.isEmpty(date)) {
            return date
        }
        date = date.split(" ".toRegex()).toTypedArray()[0] //防止格式带时分秒
        val sdf = SimpleDateFormat("yyyy-MM-dd")
        val rtn_sdf = SimpleDateFormat("MM-dd")
        return try {
            rtn_sdf.format(sdf.parse(date))
        } catch (e: ParseException) {
            date
        }
    }
    // 根据分钟数显示时长
    /**
     * 返回分钟数，如如果超过1小时就是 x小时 x分 不超过一小时就是 xx分钟
     * @param mins
     * @return
     */
//    fun getTimeStrFormSMins(mins: Double): String { //
//        var sourceString: String = (mins % 60) as Int.toString()+"分钟"
//        if (mins >= 60) {
//            sourceString = (mins % 60) as Int.toString()+"分"
//            val hourString: String = (mins / 60) as Int.toString()+"小时"
//            return hourString + sourceString
//        }
//        return sourceString
//    }

    val currentTimeWithoutYear: String
        get() = SimpleDateFormat("MM-dd HH:mm:ss").format(Date())

    fun formatDateTime(millionseconds: Long): String {
        val sdf = SimpleDateFormat(DEFALUT_DATETIMEFORMAT)
        val calendar = Calendar.getInstance()
        calendar.timeInMillis = millionseconds
        return sdf.format(calendar.time)
    }

    fun formatDateTimeWithoutYear(millionseconds: Long): String {
        val sdf = SimpleDateFormat("MM-dd HH:mm:ss")
        val calendar = Calendar.getInstance()
        calendar.timeInMillis = millionseconds
        return sdf.format(calendar.time)
    }
}